package com.whut.util;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.struts2.ServletActionContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.alibaba.fastjson.JSON;

public class UtilCommon {
	
	static int port = 25;
	static String server = "smtp.163.com";
	static String from = "Prof_Pool<prof_pool@163.com>";
	static String user = "prof_pool@163.com";
	/*	static String server = "smtp.qq.com";
	static String from = "Prof_Pool<kiwi_2010@qq.com>";
	static String user = "kiwi_2010@qq.com";*/
	static String password = "123456abc";
	
	private JavaMailSender sender;   
	  
	 public void setSender(JavaMailSender sender) {   
	  this.sender = sender;   
	 }   
	
	public static String md5Encryption(String plainText) {  
		StringBuffer buf = new StringBuffer("");  
		try {  
			MessageDigest md = MessageDigest.getInstance("MD5");  
			md.update(plainText.getBytes());  
			byte b[] = md.digest();  
			int i;  
			for (int offset = 0; offset < b.length; offset++) {  
				i = b[offset];  
				if (i < 0)  
					i += 256;  
				if (i < 16)  
					buf.append("0");  
				buf.append(Integer.toHexString(i));  
			}  
		} catch (NoSuchAlgorithmException e) {  
		// TODO Auto-generated catch block   
		e.printStackTrace();  
		}  
		return buf.toString();// 32位的加密   
		//return buf.toString().substring(8, 24);// 16位的加密   
		}  
	
	public static void sendEmail(String email, String subject, String body) {
		try {
			Properties mailProps = new Properties();
			mailProps.put("mail.smtp.host", server);
			mailProps.put("mail.smtp.port", String.valueOf(port));
			mailProps.put("mail.smtp.auth", "true");
			Transport transport = null;
			Session session = Session.getDefaultInstance(mailProps, null);
			transport = session.getTransport("smtp");
			transport.connect(server, user, password);
			MimeMessage msg = new MimeMessage(session);
			msg.setSentDate(new Date());			
			msg.setFrom(new InternetAddress(from));
			InternetAddress[] toAddress = new InternetAddress[1];
			toAddress[0] = new InternetAddress(email);
			msg.setRecipients(Message.RecipientType.TO, toAddress);
			msg.setSubject(subject, "UTF-8");	
		//	msg.setText(body, "UTF-8");
			msg.setText(body);
			msg.saveChanges();
			transport.sendMessage(msg, msg.getAllRecipients());
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
	
	public static void sendEmail2(String email, String subject, String body) {
		try {
			Properties mailProps = new Properties();
			mailProps.put("mail.smtp.host", server);
			mailProps.put("mail.smtp.port", String.valueOf(port));
			mailProps.put("mail.smtp.auth", "true");
			Transport transport = null;
			Session session = Session.getDefaultInstance(mailProps, null);
			transport = session.getTransport("smtp");
			transport.connect(server, user, password);
			MimeMessage msg = new MimeMessage(session);
			MimeMessageHelper helper = new MimeMessageHelper(msg, false, "utf-8");  
			helper.setFrom(from);   
			helper.setTo(email);   
			helper.setSubject(subject);   
			helper.setText(body, true);   		  
			transport.sendMessage(msg, msg.getAllRecipients());			
			 
		} catch (NoSuchProviderException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
	
	// 把对象封装成json对象，直接返回客户端
	public static void writeJson(Object object) {
		try {
			// SerializeConfig serializeConfig = new SerializeConfig();
			// serializeConfig.setAsmEnable(false);
			// String json = JSON.toJSONString(object);
			// String json = JSON.toJSONString(object, serializeConfig, SerializerFeature.PrettyFormat);
			String json = JSON.toJSONStringWithDateFormat(object, "yyyy-MM-dd HH:mm:ss");
			System.out.println("json:+"+json);
			// String json = JSON.toJSONStringWithDateFormat(object, "yyyy-MM-dd HH:mm:ss", SerializerFeature.PrettyFormat);
			ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(json);
			ServletActionContext.getResponse().getWriter().flush();
			ServletActionContext.getResponse().getWriter().close();
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("writeJson出异常");
		}
	} 
	
	
	

}



	

	
 
